#    Iperscribo - Creative Collaborative Writing Framework
#    Copyright (C) 2009-2010  Francesco Cabras aka Paneb [ paneb_dot_devel_at_gmail_dot_com ]
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.contrib.auth.decorators import login_required
from django.template.loader import render_to_string
from django.contrib.auth.models import User, Group
from iperscribo.projects.models import Project, Activity
from iperscribo.pages.models import Page, Tag
from django.http import HttpResponse
from django.utils import simplejson
from iperscribo.thirdparty.decorators import json_view
from iperscribo.thirdparty.djangologging import debug, info, error, warn

#################################################################################################################################################

@login_required
def add_project(request, project_name):
    
    if len(Project.objects.filter(name = project_name)) > 0:
        return HttpResponse('Error: projectname already exists')

    
    #Temporaneamente tutti gli utenti loggati possono creare progetti.
    current_project = Project(name = project_name, owner = request.user)
    current_project.save()
    #editor_group = Group(name='%s_editor' % project_name)
    #editor_group.save()
    
    #request.user.groups.add(editor_group)
    #request.user.save()
    
    request.user.add_row_perm(current_project, 'editor')
    
    return HttpResponse('Done creation of project %s' % project_name)
    
    
#################################################################################################################################################

@login_required
def view_project(request, project_name, page_path=None):

    debug('in project')

    current_project = Project.objects.get(name=project_name)
    
    if not request.user.has_row_perm(current_project, 'editor'):
        return HttpResponse('Permission Denied')
    
    
    #print project_name
    
    if page_path:
        current_page = Page.objects.get(path=page_path, project=current_project)
    
    #current_tag = Tag.objects.get(page=current_page, name='draft')
    #current_revision = current_tag.revision
    #result_text = current_tag.text
    
    
    params = {
        'project_uid' : current_project.uid,
        'project_name' : project_name,
        #'text' : result_text.replace("'", "&#39;"), #single apex replace
        #'tag_uid' : current_tag.uid,
        #'page_path' : page_path,
        'view_type' : 'status',
        'user' : request.user,
    }
    
    javascript_render = render_to_string('project/project.js', params)
    params.update({'javascript_render' : javascript_render})
    return HttpResponse(render_to_string('project/newproject.html', params))

#################################################################################################################################################

@login_required
def view_project_status(request, projectname):
    
    recent_activities = Activity.objects.all().order_by('-action_date')[:10]
    
    return HttpResponse(render_to_string('project/status.html', {'project_name' : projectname, 'recent_activities' : recent_activities}))

#################################################################################################################################################


@login_required
def get_tree(request, project_name):
    
    print 'in get tree', project_name
    current_project = Project.objects.get(name = project_name)
    pages = Page.objects.filter(project=current_project)
    
    pages_list = [{'id' : 'iperscribo:status', 'text' : 'Project Status', 'leaf' : True, 'type' : 'project_status', 'iconCls': 'tree_status'}]
    for page in pages:
        pages_list.append({'id' : page.uid, 'text' : page.path, 'leaf' : True, 'type' : 'page'})
    
    
    return HttpResponse(simplejson.dumps(pages_list))
